package domain.util;

import java.util.Scanner;

import domain.Query;
import domain.Resource;
import facade.Facade;

public class ParserGenerated {
	
	public static void put(Resource resource, String key, String value) {
		System.out.println("key = " + key);
		System.out.println("value = " + value);
		
		if (key.equals("245$a")) {
			resource.setTitle(value);
		}
		if (key.equals("100$a")) {
			resource.setCreator(value);
		}
		if (key.equals("110$a")) {
			resource.setCreator(value);
		}
		if (key.equals("111$a")) {
			resource.setCreator(value);
		}
		if (key.equals("024$a")) {
			resource.setCutter(value);
		}
		if (key.equals("020$a")) {
			resource.setCutter(value);
		}
		if (key.equals("022$a")) {
			resource.setCutter(value);
		}
		if (key.equals("260$c")) {
			resource.setDate(value);
		}
		if (key.equals("700$a")) {
			resource.setContributor(value);
		}
		if (key.equals("710$a")) {
			resource.setContributor(value);
		}
		if (key.equals("711$a")) {
			resource.setContributor(value);
		}
		if (key.equals("786$a")) {
			resource.setSource(value);
		}
		if (key.equals("080$a")) {
			resource.setCduClassification(value);
		}
		if (key.equals("520$a")) {
			resource.setDescription(value);
		}
		if (key.equals("505$a")) {
			resource.setDescription(value);
		}
		if (key.equals("044$a")) {
			resource.setCountry(value);
		}
		if (key.equals("546$a")) {
			resource.setLanguageName(value);
		}
		if (key.equals("260$b")) {
			resource.setPublisherName(value);
		}
		if (key.equals("260$a")) {
			resource.setPublisherPlace(value);
		}
		if (key.equals("440$v")) {
			resource.setVolumeNumber(value);
		}
		if (key.equals("020$a")) {
			resource.setIsbn(value);
		}
		if (key.equals("022$a")) {
			resource.setIssn(value);
		}
		//resource.setMarcCode( (resource.getMarcCode() + "\n" + key + "\t" + value).trim() );
	}
	
	public static Resource getResourceFromMarc(String marc) {
		Resource result = new Resource();
		
		Scanner sc = new Scanner(marc);
		
		while (sc.hasNextLine()) {
			String line = sc.nextLine();

			String[] tokens = line.split("\\s+");
			
			if (tokens.length == 2) {
				put(result, tokens[0], tokens[1]);
				continue;
			}
			
			String code = tokens[0];
			
			int j = 0;
			
			boolean hasParagraph = false;
			boolean hasLiteral = false;
			
			String paragraph = null;
			String literal = "";
			
			while (++j < tokens.length) {
				if (tokens[j].startsWith("$")) {
					if (hasParagraph && hasLiteral) {
						put(result, code + paragraph, literal);
						hasParagraph = hasLiteral = false;
						literal = "";
					}
					paragraph = tokens[j];
					hasParagraph = true;
				} else {
					if (hasParagraph) {
						literal += " " + tokens[j];
						literal = literal.trim();
						hasLiteral = true;
					}
				}
			}
			
			if (hasParagraph && hasLiteral) {
				put(result, code + paragraph, literal);
				hasParagraph = hasLiteral = false;
				literal = "";
			}
		}
		
		return result;
	}
	
	public static void main(String[] args) {
		Query q = new Query();
		q.setQuery("Date C. J.");
		q.setMode(1);
		
		Resource r = getResourceFromMarc(Facade.getInstance().search(q, 25));
		
		System.out.println(r.getTitle());
		System.out.println(r.getCreator());
	}
	
}
